package com.stormrage.edu.ctrl.learn;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.stormrage.edu.serv.learn.LeaStudentServ;
import com.stormrage.entity.learn.req.LeaApplyReq;
import com.stormrage.entity.rep.ErrorRep;
import com.stormrage.entity.shiro.ShiroAcct;
import com.stormrage.shiro.bind.CurrentUser;
import com.stormragetech.core.ctrl.BaseCtrl;

/**
 * @Title: EduLecCtrl.java
 * @Package com.stormrage.edu.ctrl.edu
 * @Description: 报名 Controller
 * @author www.stormragetech.com
 * @date 2016年4月16日
 * @version V1.0
 */
@Controller
@RequestMapping("/apply")
public class LeaApplyCtrl extends BaseCtrl {

	private static final Logger logger = LoggerFactory.getLogger(LeaApplyCtrl.class);

	@Autowired
	private LeaStudentServ leaStudentServ;

	@RequestMapping(value = "/{id}", method = RequestMethod.GET)
	public String major(@CurrentUser ShiroAcct curr, @PathVariable("id") Integer id, RedirectAttributes redirect) {

		logger.debug(" 【 Controller 】- 【 LearnApplyCtrl 】- 【 Apply 】-【 GET 】-【 STARTED 】");

		ErrorRep err = null;
		String errMsg = "";

		try {

			// ------------------ 报名 ------------------
			LeaApplyReq req = LeaApplyReq.instance(Integer.valueOf(curr.getId()), id);
			leaStudentServ.create(req);

			// ------------------ 赋值 Model ------------------
			errMsg = "报名成功";
			err = ErrorRep.instance(true, errMsg);
			redirect.addFlashAttribute("err", err);

		} catch (Exception e) {
			err = ErrorRep.instance(false, e.getMessage());
			redirect.addFlashAttribute("err", err);
			return "redirect:/result";
		}

		logger.debug(" 【 Controller 】- 【 LearnApplyCtrl 】- 【 Apply 】-【 GET 】-【 COMPLETED 】");
		return "redirect:/result";

	}
}
